// pages/order/orderlist.js
import http from '../../utils/http.js'
import moment from '../../utils/moment.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: app.globalData.imgDomain,
    page:1,
    pageSize:5,
    orderList:[]
  },
  openDetail(e){
    wx.navigateTo({
      url: './order_detail?orderCode='+e.currentTarget.dataset.orderCode,
    })
  },
  payOrder(e){
    let me=this;
    http.request({
      url: 'wxPay/wxPrePay',
      data: {
        tradeId:e.currentTarget.dataset.order.orderSn,
        body:'浮遇纪Cake',
        wxKey: app.getSessionKey(),
        againCreateOrder:true
      },
      success: res => {
        if (res.code == "200") {
          if (res.msg) {
            var msg = res.msg;
            wx.requestPayment({
              timeStamp: msg.timeStamp.toString(),
              nonceStr: msg.nonceStr,
              package: msg.package,
              signType: 'MD5',
              paySign: msg.sign,
              success: res => {

              },
              fail: res => {
                if (res.errMsg === 'requestPayment:fail cancel') {
                  wx.showToast({
                    icon: 'none',
                    title: '用户取消支付'
                  })
                  me.getOrderList();
                }
              },
            })
          }
        }
      }
    });
  },
  getOrderList(){
    let me=this;
    let userId = wx.getStorageSync("userId");
    http.request({
      url:'order/getListForWx',
      loadTip: '加载中',
      data:{
        page: me.data.page,
        pageSize:me.data.pageSize,
        userId:userId,
        integral:1
      },
      success:res=>{
        me.setData({
          orderList:res.list
        });
      }
    });
  },
  cancelOrder(e){
    let me=this;
    wx.showModal({
      title: '订单操作',
      content: '您确定要取消该订单吗？',
      success:res=>{
        if(res.confirm){
          http.request({
            url:'order/cancel/'+e.currentTarget.dataset.orderId,
            success:res=>{
              wx.showToast({
                title: '取消成功'
              })
              me.getOrderList();
            }
          });
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrderList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: '我的订单'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})